﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using estudent.Models;

namespace estudent.Controllers
{
    public class DrzavaSifrantController : Controller
    {
        private estudentEnt db = new estudentEnt();
        int? stara = null;
        //
        // GET: /DrzavaSifrant/

        public ActionResult Index()
        {
            return View(db.SDrzaves.ToList());
        }

        //
        // GET: /DrzavaSifrant/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /DrzavaSifrant/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /DrzavaSifrant/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "veljavno")] SDrzave ustvarjena)
        {
            if (ustvarjena.sDrzava.ToString().Length > 3)
                ModelState.AddModelError("sDrzava", "Predolgo");
            try
            {
                if (!ModelState.IsValid)
                    return View();

                db.AddToSDrzaves(ustvarjena);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("sDrzava", "Ni unikat");
                return View();
            }
        }
        
        //
        // GET: /DrzavaSifrant/Edit/5
 
        public ActionResult Edit(int id)
        {
            var uredi = (from p in db.SDrzaves
                         where p.sDrzava == id
                         select p).First();
            stara = id;

            return View(uredi);
        }

        //
        // POST: /DrzavaSifrant/Edit/5

        [HttpPost]
        public ActionResult Edit([Bind(Exclude = "veljavno")] SDrzave urejen)
        {
            if (urejen.sDrzava.ToString().Length > 3)
                ModelState.AddModelError("sDrzava", "Predolgo");
            if (urejen.sDrzava != stara && db.SDrzaves.Any(p => p.sDrzava == urejen.sDrzava))
                ModelState.AddModelError("sObcina", "Obstaja");
            try
            {
                var original = (from p in db.SDrzaves
                                where p.sDrzava == urejen.sDrzava
                                select p).First();

                if (!ModelState.IsValid)
                    return View(original);

                db.ApplyCurrentValues(original.EntityKey.EntitySetName, urejen);
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("sDrzava", "Ni unikat");
                return View();
            }
        }

        //
        // GET: /DrzavaSifrant/Delete/5
 
        public ActionResult Delete(int id)
        {
            try
            {
                var orig = (from p in db.SDrzaves
                            where p.sDrzava == id
                            select p).First();
                var spremen = (from p in db.SDrzaves
                               where p.sDrzava == id
                               select p).First();

                if (orig.veljavno != null)
                {
                    spremen.veljavno = !orig.veljavno;
                }
                else
                {
                    spremen.veljavno = false;
                }

                db.ApplyCurrentValues(orig.EntityKey.EntitySetName, spremen);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /DrzavaSifrant/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
